package com.hd.trans.dialog

import android.content.Context
import android.os.SystemClock
import android.view.LayoutInflater
import android.view.View
import androidx.core.view.isVisible
import androidx.core.widget.NestedScrollView
import com.github.penfeizhou.animation.apng.APNGDrawable
import com.github.penfeizhou.animation.loader.ResourceStreamLoader
import com.hd.trans.R
import com.hd.trans.TranslationInterior
import com.hd.trans.binding.setOnDebouncedClickListener
import com.hd.trans.databinding.PopTransVoiceRecordingBinding
import com.hd.trans.framework.dialog.CstBindPop
import com.hd.trans.utils.TrackerForTrans
import com.hd.trans.utils.VirtualToast

class HomeVoiceRecordingPop(context: Context) : CstBindPop<PopTransVoiceRecordingBinding>(
    LayoutInflater.from(context).inflate(R.layout.pop_trans_voice_recording, null), false
) {

    private val mVirtualToast by lazy {
        VirtualToast(mDataBinding.tvToast)
    }

    private val mAnimation by lazy {
        APNGDrawable(ResourceStreamLoader(mContext, R.raw.trans_ic_voice_animation))
    }

    private var isAutoScroll = true

    init {

        mDataBinding.apply {
            tvStop.setOnDebouncedClickListener {
                TranslationInterior.getTrackerListener()
                    .click(TrackerForTrans.YYFY, "语音翻译-录音-完成")
                dismiss()
            }
            ivClose.setOnDebouncedClickListener {
                dismiss()
            }

            ivAnimation.setImageDrawable(mAnimation)
            scrollRecognition.setOnScrollChangeListener(
                NestedScrollView.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY -> //如果是向上滑动，isAutoScroll设为false
                    //如果是向下滑动，并且已滑动到底部，isAutoScroll设为true

                    if (scrollY < oldScrollY) {
                        isAutoScroll = false
                    } else {
                        val isBottom =
                            scrollRecognition.scrollY + scrollRecognition.height >= scrollRecognition.getChildAt(
                                0
                            ).measuredHeight
                        if (isBottom) {
                            isAutoScroll = true
                        }
                    }
                })
        }

    }

    fun setRecognitionContent(content: String){
        val contentTrim = content.trim()
        mDataBinding.tvContentRecognition.text = contentTrim
        mDataBinding.tvSpeakTips.isVisible = contentTrim.isEmpty()
        if (isAutoScroll) {
            mDataBinding.tvContentRecognition.post {
                //NestedScrollView滚动到底部
                mDataBinding.scrollRecognition.fullScroll(View.FOCUS_DOWN)
            }
        }
    }

    fun getRecognitionContent(): String {
        return mDataBinding.tvContentRecognition.text.toString()
    }

//    fun setLanguage(language: HuDunLanguage){
//        TranslateContentConstants.listeningPleaseSpeak(language){
//            val tips = String.format(it,"#4990F7")
//            val topsHtml = HtmlCompat.fromHtml(tips)
//            mDataBinding.tvSpeakTips.text = topsHtml
//        }
//        TranslateContentConstants.complete(language){
//            mDataBinding.tvStop.text = it
//        }
//    }

    fun resetTimeChronometer(){
        setRecognitionContent("")
        mDataBinding.timeChronometer.base = SystemClock.elapsedRealtime() //计时器清零
        mDataBinding.timeChronometer.start()
    }

    fun showToast() {
        mVirtualToast.showAlways("请靠近麦克风并提高音量")
    }

    fun hideToast(){
        mVirtualToast.cancel()
    }

    override fun onDismiss() {
        super.onDismiss()
        mAnimation.stop()
        mVirtualToast.cancel()
    }

    fun show() {
        setRecognitionContent("")
        showFromBottom()
        mAnimation.start()
    }

}